package common.controls.item
{
	import codes.SocketCodes;
	
	import com.netease.protobuf.Int64;
	
	import common.controls.item.attribute.ItemAttribute;
	import common.controls.item.events.ItemDataEvent;
	
	import flash.events.EventDispatcher;
	
	import kernel.config.ConfigData;
	import kernel.core.Core;
	import kernel.events.ServerEvent;
	import kernel.server.Server;
	
	import proto.client.ItemInfo;
	import proto.client.Money;
	import proto.client.SSC000100;
	import proto.client.SSC000101;
	import proto.client.SSC000102;
	import proto.client.SSC000103;
	import proto.client.SSC000105;
	import proto.client.SSC000107;
	
	[Event(name="changedgold", type="common.events.ItemDataEvent")]
	[Event(name="changedsilver", type="common.events.ItemDataEvent")]
	[Event(name="changeditemupdate", type="common.events.ItemDataEvent")]
	
	[Event(name="changedstack", type="common.events.ItemAttributeEvent")]
	
	/**  
	 * 数据管理器
	 * @author carrey
	 * */ 
	public class ItemDataManager extends EventDispatcher
	{
		private static var _ins:ItemDataManager;
		
		private var _dataList:Vector.<ItemAttribute>;//道具
		private var _totalGrid:uint;//已经开启方格总数量
		private var _openGridTime:uint;//开启方格时间--秒
		private var _gold:uint;//金币
		private var _silver:uint;//银币
		
		private var _itemsXml:XML;
		
		public function ItemDataManager()
		{
			_dataList = new Vector.<ItemAttribute>();
			
			_totalGrid = 0;
			_openGridTime = 0;
			
			Server.ins.addEventListener(SocketCodes.S_0x000100,onS_0x001000);
			Server.ins.addEventListener(SocketCodes.S_0x000101,onS_0x001001);
			Server.ins.addEventListener(SocketCodes.S_0x000102,onS_0x001002);
			Server.ins.addEventListener(SocketCodes.S_0x000103,onS_0x001003);
			Server.ins.addEventListener(SocketCodes.S_0x000105,onS_0x001005);
			Server.ins.addEventListener(SocketCodes.S_0x000107,onS_0x001007);
			Server.ins.addEventListener(SocketCodes.S_0x000002,onS_0x000002);
			
			_itemsXml = ConfigData.getXML("items");
			
			var playerObj:Object = Core.all.getByKey ("loginedPlayer");
			
			if (playerObj)
			{
				_gold = playerObj["gold"];
				_silver = playerObj["silver"];
			}
		}

		public function get silver():uint
		{
			return _silver;
		}

		public function set silver(value:uint):void
		{
			_silver = value;
		}

		public function get gold():uint
		{
			return _gold;
		}

		public function set gold(value:uint):void
		{
			_gold = value;
		}

		public function get openGridTime():uint
		{
			return _openGridTime;
		}

		public function set openGridTime(value:uint):void
		{
			_openGridTime = value;
		}

		public function get totalGrid():uint
		{
			return _totalGrid;
		}

		public function set totalGrid(value:uint):void
		{
			_totalGrid = value;
		}

		public function get dataList():Vector.<ItemAttribute>
		{
			return _dataList;
		}

		public function set dataList(value:Vector.<ItemAttribute>):void
		{
			_dataList = value;
		}

		public static function get instance():ItemDataManager
		{
			if( _ins==null )
			{
				_ins = new ItemDataManager();
			}
			return _ins;
		}
		public function getItemData(id:Int64):ItemAttribute
		{
			var len:uint=_dataList.length;
			var item:ItemAttribute;
			for(var i:uint=0;i<len;i++)
			{
				if(_dataList[i].id.toString()==id.toString())
				{
					return _dataList[i];
				}
			}
			return null;
		}
		public function getItemDataByPosition(pos:uint):ItemAttribute
		{
			var len:uint=_dataList.length;
			var item:ItemAttribute;
			for(var i:uint=0;i<len;i++)
			{
				if(_dataList[i].position==pos)				{
					return _dataList[i];
				}
			}
			return null;
		}
		private function clearItemData():void
		{
			var len:uint=_dataList.length;
			for(var i:uint=0;i<len;i++)
			{
				_dataList.pop();
			}
		}
		private function delItemData(id:Int64):void
		{
			var len:uint=_dataList.length;
			for(var i:uint=0;i<len;i++)
			{
				if(_dataList[i].id==id)
				{
					_dataList.splice(i,1);
				}
			}
		}
		/**
		 * 初始化背包
		 * 
		 */		
		private function onS_0x001000(evt:ServerEvent):void
		{
			var data:SSC000100 = evt.createMessage(SSC000100);
			if(data)
			{
				_totalGrid = data.slotTotal;
				_openGridTime = data.openSlotTime;
				var len:uint=data.itemInfo.length;
				var item:ItemAttribute;
				var info:ItemInfo;
				for(var i:uint=0;i<len;i++)
				{
					info = data.itemInfo[i];
					
					item = getItem(info.entryId);
					item.id=info.id;
					item.type=info.type;
					item.bindType=info.bindType;
					item.name=info.name;
					item.setStack(info.stack);
					item.isActivate=info.isActivate;
					item.activateTime=info.activateTime;
					item.position=info.slot;
					item.containerType=info.containerType;
					
					_dataList.push(item);
				}
				dispatchEvent(new ItemDataEvent(ItemDataEvent.CHANGED_ITEM_UPDATE));
			}
		}
		public function getItem(id:uint):ItemAttribute
		{
			var item:ItemAttribute= new ItemAttribute();
			var itemxml:XML = _itemsXml.item.(@id == id)[0];
			if(itemxml != null)
			{
				item.itemId=itemxml.@id;
				item.type=itemxml.@type;
				item.sex=itemxml.@sex;
				item.name=itemxml.name;
				item.useLevel=itemxml.useLevel;
				item.currencyNum=itemxml.currencyNum;
				item.currencyType=itemxml.currencyType;
				item.stackTotal=itemxml.stackTotal;
				item.renewal=itemxml.renewal;
				item.renewalNum=itemxml.renewalNum;
				item.description=itemxml.description;
				item.charm=itemxml.charm;
			}
			return item;
		}
		/**
		 * 移动道具
		 * 
		 */		
		private function onS_0x001001(evt:ServerEvent):void
		{
			var data:SSC000101 = evt.createMessage(SSC000101);
			if(data)
			{
				var item:ItemAttribute = getItemData(data.itemId);
				var pitem:ItemAttribute = getItemDataByPosition(data.containerSlot);
				
				if(data.containerType==ItemContainerType.BACKPACK)
				{
					if(pitem == null)
					{
						if(item != null)
						{
							item.position = data.newSlot;
						}
					}else{
						pitem.position = item.position;
						if(item != null)
						{
							item.position = data.newSlot;
						}
					}
					dispatchEvent(new ItemDataEvent(ItemDataEvent.CHANGED_ITEM_MOVE,data));
				}else{
					dispatchEvent(new ItemDataEvent(ItemDataEvent.CHANGED_EQUIP_ITEM_MOVE,data));
				}
			}
		}
		/**
		 * 删除道具 
		 * 
		 */		
		private function onS_0x001002(evt:ServerEvent):void
		{
			var data:SSC000102 = evt.createMessage(SSC000102);
			if(data)
			{
				if(data.containerType==ItemContainerType.BACKPACK)
				{
					delItemData(data.itemId);
					dispatchEvent(new ItemDataEvent(ItemDataEvent.CHANGED_ITEM_DELETE,data));
				}else{
					dispatchEvent(new ItemDataEvent(ItemDataEvent.CHANGED_EQUIP_ITEM_DELETE,data));
				}
			}
		}
		/**
		 * 整理道具容器
		 * 
		 */		
		private function onS_0x001003(evt:ServerEvent):void
		{
			var data:SSC000103 = evt.createMessage(SSC000103);
			if(data)
			{
				clearItemData();
				var len:uint=data.itemInfo.length;
				var item:ItemAttribute;
				var info:ItemInfo;
				for(var i:uint=0;i<len;i++)
				{
					info = data.itemInfo[i];
					
					item = getItem(info.entryId);
					item.id=info.id;
					item.type=info.type;
					item.bindType=info.bindType;
					item.name=info.name;
					item.setStack(info.stack);
					item.isActivate=info.isActivate;
					item.activateTime=info.activateTime;
					item.position=info.slot;
					item.containerType=info.containerType;
					
					_dataList.push(item);
				}
				dispatchEvent(new ItemDataEvent(ItemDataEvent.CHANGED_ITEM_UPDATE));
			}
		}
		/**
		 * 创建物品
		 * 
		 */		
		private function onS_0x001005(evt:ServerEvent):void
		{
			var data:SSC000105 = evt.createMessage(SSC000105);
			if(data)
			{
				var item:ItemAttribute = getItem(data.itemInfo.entryId);
				item.id=data.itemInfo.id;
				item.type=data.itemInfo.type;
				item.bindType=data.itemInfo.bindType;
				item.name=data.itemInfo.name;
				item.setStack(data.itemInfo.stack);
				item.isActivate=data.itemInfo.isActivate;
				item.activateTime=data.itemInfo.activateTime;
				item.position=data.itemInfo.slot;
				item.containerType=data.itemInfo.containerType;
				if(data.itemInfo.containerType==ItemContainerType.BACKPACK)
				{
					_dataList.push(item);
					dispatchEvent(new ItemDataEvent(ItemDataEvent.CHANGED_ITEM_UPDATE));
				}else{
					dispatchEvent(new ItemDataEvent(ItemDataEvent.CHANGED_EQUIP_ITEM_CREATE,item));
				}
			}
		}
		/**
		 * 更新叠加数量
		 * 
		 */		
		private function onS_0x001007(evt:ServerEvent):void
		{
			var data:SSC000107 = evt.createMessage(SSC000107);
			if(data)
			{
				var item:ItemAttribute = getItemData(data.itemId);
				if(item != null)
				{
					item.setStack(data.newStack,true);
				}
				dispatchEvent(new ItemDataEvent(ItemDataEvent.CHANGED_ITEM,item));
			}
		}
		/**
		 * 更新货币
		 * 
		 */		
		private function onS_0x000002(evt:ServerEvent):void
		{
			var data:Money = evt.createMessage(Money);
			if(data)
			{
				if(data.hasGold)
				{
					if(_gold != data.gold)
					{
						_gold = data.gold;
						dispatchEvent(new ItemDataEvent(ItemDataEvent.CHANGED_GOLD,_gold));
					}
				}
				if(data.hasSilver)
				{
					if(_silver != data.silver)
					{
						_silver = data.silver;
						dispatchEvent(new ItemDataEvent(ItemDataEvent.CHANGED_SILVER,_silver));
					}
				}
			}
		}
	}
}








